/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package control;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import entity.Movie;
import entity.Session;
/**
 *
 * @author 51436342
 */
@WebServlet(name="movieRetrieve", urlPatterns={"/movieRetrieve.do"})
public class movieRetrieve extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            //get all movies from servlet
            Context initCtx = new InitialContext();
            Context envCtx = (Context)initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource)envCtx.lookup("jdbc/phonebook");
            Connection con = ds.getConnection();
            Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            ResultSet rs = stmt.executeQuery("SELECT * FROM [movie] ORDER BY [id] ASC");
            int movieNum=0;
            if (rs != null && rs.last() != false) {
                movieNum = rs.getRow();
                rs.beforeFirst();
            }
            int[] id=new int[movieNum];
            String[] name=new String[movieNum];
            int[] duration=new int[movieNum];
            String[] genre=new String[movieNum];
            String[] opendate=new String[movieNum];
            String[] movielang=new String[movieNum];
            String[] actor=new String[movieNum];
            String[] storyline=new String[movieNum];

            Movie[] movies=new Movie[movieNum];
            
            int i=0;
            while (rs != null && rs.next() != false) {
                id[i]=rs.getInt("id");
                name[i] = rs.getString("name");
                duration[i]=rs.getInt("duration");
                genre[i]=rs.getString("genre");
                opendate[i] = rs.getString("opendate");
                movielang[i]=rs.getString("movielang");
                actor[i]=rs.getString("actor");
                storyline[i]=rs.getString("storyline");
                i++;
            }
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (con != null) {
                con.close();
            }

            //for each movie, cosntruct the object of the movie
            for(int j=0;j<movieNum;j++)
            {
                movies[j]=new Movie(id[j],name[j],duration[j],genre[j],opendate[j],movielang[j],actor[j],storyline[j]);

            }

           //get all sessions from servlet
           con = ds.getConnection();
           stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
           rs = stmt.executeQuery("SELECT * FROM [screening] ORDER BY [id] ASC");
           int sessionNum=0;
            if (rs != null && rs.last() != false) {
                sessionNum = rs.getRow();
                rs.beforeFirst();
            }
            int[] sessionId=new int[sessionNum];
            int[] movieId=new int[sessionNum];
            String[] showTime=new String[sessionNum];
            int[] houseId=new int[sessionNum];
            int[] price=new int[sessionNum];
            String[] seating=new String[sessionNum];

            Session[] sessions=new Session[sessionNum];

            i=0;
            while (rs != null && rs.next() != false) {
                sessionId[i]=rs.getInt("id");
                movieId[i] = rs.getInt("movieid");
                showTime[i]=rs.getString("showtime");
                houseId[i]=rs.getInt("houseid");
                price[i]=rs.getInt("price");
                seating[i]=rs.getString("seating");
                i++;
            }
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (con != null) {
                con.close();
            }

            Movie temp=new Movie();
            //construct all sessions and add session for the movie
            for (int j=0;j<sessionNum;j++)
            {
                sessions[j]=new Session(sessionId[j],movieId[j],showTime[j],houseId[j],price[j],seating[j]);
                temp=temp.getMovieById(movieId[j]);
                temp.addSession(sessionId[j]);       
            }



            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet movieRetrieve</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet movieRetrieve at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        }catch (NamingException e) {
            out.println("<div style='color: red'>" + e.toString() + "</div>");
        }catch (SQLException e) {
            out.println("<div style='color: red'>" + e.toString() + "</div>");
        }finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
